Workaround bug in xmlrpclib's string escaping. That library outputs invalid
authoremellor@leeni.uk.xensource.com <emellor@leeni.uk.xensource.com>
Tue, 4 Apr 2006 10:08:20 +0000 (11:08 +0100)
committeremellor@leeni.uk.xensource.com <emellor@leeni.uk.xensource.com>
Tue, 4 Apr 2006 10:08:20 +0000 (11:08 +0100)
UTF-8 if given a string containing high-bit characters, so instead pass in
Unicode strings, for which the escaping is correct.

This fixes a bug seen when the dmesg of a machine contains high bit characters,
but I'm sure there are other ways in which it might be triggered.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
tools/python/xen/util/xmlrpclib2.py

index c0f769f6f23ff6d6dc60aa191b3be1e02ddc93a0..65aa49ced53b99620ef3b685667560b085e15c83 100644 (file)
@@ -20,6 +20,8 @@
 An enhanced XML-RPC client/server interface for Python.
 """
 
+import types
+
 from httplib import HTTPConnection, HTTP
 from xmlrpclib import Transport
 from SimpleXMLRPCServer import SimpleXMLRPCServer, SimpleXMLRPCRequestHandler
@@ -77,6 +79,15 @@ class TCPXMLRPCServer(SocketServer.ThreadingMixIn, SimpleXMLRPCServer):
             else:
                 response = self._dispatch(method, params)
 
+            # Convert strings to unicode strings so that they are escaped
+            # properly by xmlrpclib.  We use latin-1 here, but any
+            # ASCII-compatible scheme would do -- we just care about getting
+            # the bytes across the wire.
+            # Any message handler that actually cares about the charset in
+            # use should be returning Unicode strings.
+            if isinstance(response, types.StringType):
+                response = unicode(response, 'iso-8859-1')
+
             response = (response,)
             response = xmlrpclib.dumps(response,
                                        methodresponse=1,